import { AssetacquisitionManageAPI } from '../common/type';
import { Button, Tooltip } from 'antd';
import { getColorTextLoadMap, getTenantID, getUserID, removeDateHmsStr } from '@/utils';
import pageStyles from '@/common/pageLayout.less';
import { Fragment } from 'react';
import { EditOutlined, DownloadOutlined, DeleteOutlined } from '@ant-design/icons';
import { stateOfRendering } from '../common';
import { ManageCommonAPI } from '@/pages/type';
import { tableMoreBtnConfig } from '@/utils/myutilConfig';
import { ColumnsRenderFun, DictionaryRenderFun, ILoadMapTypeAll } from '@/common';
import moment from 'moment';
import MyIcon from '@/components/MyIcon';

/**操作按钮的显示*/
const Operation = (curRes: Record<string, any>, operateFuns: AssetacquisitionManageAPI.IMaterialTableActionType, record: Record<string, any>) => {
  let operationArray: any = [];

  /** 一个按钮都没有时展示*/
  const Disable = (
    <Fragment key={record?.id + '--Disable'}>
      <Button style={{ color: '#000' }} className="manageBtntTxtStyle" type={'link'} size={'small'} disabled>
        --
      </Button>
    </Fragment>
  );

  /** 管理页操作栏--编辑按钮权限判断 */
  const onEditVisible = () => {
    const editVisible: boolean =
      (curRes?.['/assetInventory/edit'] && !record?.checkState && record?.state == `${getTenantID()}${ILoadMapTypeAll.ASSET_INPLAY_STATUS}1000`) ||
      (record?.checkState == `${getTenantID()}${ILoadMapTypeAll.ALLFLOW_CHECKRESULT}1003` &&
        record?.state == `${getTenantID()}${ILoadMapTypeAll.ASSET_INPLAY_STATUS}1002`);
    return editVisible;
  };

  /** 管理页操作栏--提交按钮权限判断 */
  const onSubmitVisible = () => {
    const submitVisible: boolean =
      (!record?.checkState && record?.state == `${getTenantID()}${ILoadMapTypeAll.ASSET_INPLAY_STATUS}1000`) ||
      (record?.checkState == `${getTenantID()}${ILoadMapTypeAll.ALLFLOW_CHECKRESULT}1003` &&
        record?.state == `${getTenantID()}${ILoadMapTypeAll.ASSET_INPLAY_STATUS}1002`);
    return submitVisible;
  };

  /** 管理页操作栏--撤销按钮权限判断 */
  const onRevokeVisible = () => {
    return (
      record?.checkState == `${getTenantID()}${ILoadMapTypeAll.ALLFLOW_CHECKRESULT}1000` &&
      record?.state == `${getTenantID()}${ILoadMapTypeAll.ASSET_INPLAY_STATUS}1001` &&
      record?.createrID == getUserID()
    );
  };

  /** 更多按钮配置 */
  const otherBtnConfig: ManageCommonAPI.TableMoreBtnConfigApi[] = [
    {
      icon: <EditOutlined />,
      btnName: '编辑',
      clickFnc: operateFuns,
      clickType: 'onEdit',
      isVisible: onEditVisible(),
    },
    {
      icon: <DeleteOutlined />,
      btnName: '删除',
      clickFnc: operateFuns,
      clickType: 'onCancel',
      isVisible: onSubmitVisible(),
    },
    {
      icon: <DownloadOutlined />,
      btnName: '导出',
      clickFnc: operateFuns,
      clickType: 'onExport',
      // isVisible: curRes?.['/assetInventory/export'] && (record?.state == 0 || record?.state == 2),
      isVisible: (curRes?.['/assetInventory/export/{id}'] && record?.state == `${getTenantID()}${ILoadMapTypeAll.ASSET_INPLAY_STATUS}1003`) || false,
    },
    // {
    //   icon: <RollbackOutlined />,
    //   btnName: '撤销',
    //   clickFnc: operateFuns,
    //   clickType: 'onRevoke',
    //   isVisible: onRevokeVisible(),
    // },
    // {
    //   icon: <CheckCircleOutlined />,
    //   btnName: '完成',
    //   clickFnc: operateFuns,
    //   clickType: 'onComplete',
    //   isVisible: (curRes?.['/assetInventory/terminate/{id}'] && record?.state == `${getTenantID()}${ILoadMapTypeAll.ASSET_INPLAY_STATUS}1003`) || false,
    // },
    // {
    //   icon: <ContainerOutlined />,
    //   btnName: 'OA',
    //   clickFnc: operateFuns,
    //   clickType: 'onOAdetail',
    //   // 没有审批状态 并且 申请状态在起草  显示可以提交
    //   isVisible: true,
    // },
  ];

  const ohterBtn = tableMoreBtnConfig(otherBtnConfig, record);
  operationArray = ohterBtn;
  operationArray = operationArray?.filter((itemBtn: any) => itemBtn);
  return operationArray && operationArray?.length > 0 ? operationArray : [Disable];
};

// const { loadMapInfo, mapLoading } = getLoadMapList([
//   `${getTenantID()}${ILoadMapTypeAll.ASSET_INNENTORY_STATUS}`, //资产类型
// ]);
// const typeList = getSelectDataMapList(loadMapInfo?.[`${getTenantID()}${ILoadMapTypeAll.ASSET_INNENTORY_STATUS}`], 'name', 'id', true);
// console.log('123',typeList)

// Table表格的配置
const getTableColumns = (curRes: Record<string, any>, operateFuns: AssetacquisitionManageAPI.IMaterialTableActionType) => {
  return [
    {
      title: '申请部门/单位',
      dataIndex: 'createDepartmentName',
      key: 'createDepartmentName',
      fixed: 'left',
      width: 150,
      sorter: (a: any, b: any) => (a?.createDepartmentName > b?.createDepartmentName ? 1 : -1),
      render: (text: string, record: any) => ColumnsRenderFun({ text, isTooltip: true }),
    },
    {
      title: '事项名称',
      dataIndex: 'inventoryTitle',
      key: 'inventoryTitle',
      width: 150,
      visible: true,
      unDrag: true,
      fixed: 'left',
      render: (text: string | null, record: Record<string, any>) => {
        return (
          <Tooltip title={text} placement="top">
            <span className={pageStyles['over-flow-hidden']} style={{ maxWidth: '100%' }} id={record?.id}>
              <a onClick={() => operateFuns?.onView(record)}>{text || '--'}</a>
            </span>
          </Tooltip>
        );
      },
    },
    {
      title: '盘点说明',
      dataIndex: 'inventoryInfo',
      key: 'inventoryInfo',
      width: 150,
      visible: true,
      sorter: (a: any, b: any) => (a?.inventoryInfo > b?.inventoryInfo ? 1 : -1),
      render: (text: string, record: any) => ColumnsRenderFun({ text, isTooltip: true }),
    },
    // {
    //   title: '申请人',
    //   dataIndex: 'createrName',
    //   key: 'createrName',
    //   visible: true,
    //   width: 100,
    //   render: (text: string | null, record: Record<string, any>) => ColumnsRenderFun({ text, isTooltip: true }),
    // },
    // {
    //   title: '申请时间',
    //   dataIndex: 'createTime',
    //   key: 'createTime',
    //   visible: true,
    //   width: 100,
    //   sorter: (a: any, b: any) => (a?.createTime > b?.createTime ? 1 : -1),
    //   render: (text: string, record: Record<string, any>) => {
    //     return (
    //       <Tooltip title={text} placement="top">
    //         <span className={pageStyles['over-flow-hidden']} style={{ maxWidth: '100%' }} id={record?.id}>
    //           {text ? moment(text).format('YYYY-MM-DD') : '--'}
    //         </span>
    //       </Tooltip>
    //     );
    //   },
    // },
    {
      title: '盘点状态',
      dataIndex: 'stateName',
      key: 'stateName',
      visible: true,
      width: 100,
      render: (text: number) => DictionaryRenderFun({ text, isTooltip: true, isState: true }),
    },
    {
      title: '开始时间',
      dataIndex: 'startTime',
      key: 'startTime',
      visible: true,
      width: 150,
      sorter: (a: any, b: any) => (a?.startTime > b?.startTime ? 1 : -1),
      render: (text: string, record: Record<string, any>) => {
        return (
          <Tooltip title={text} placement="top">
            <span className={pageStyles['over-flow-hidden']} style={{ maxWidth: '100%' }} id={record?.id}>
              {text ? moment(text).format('YYYY-MM-DD') : '--'}
            </span>
          </Tooltip>
        );
      },
    },
    {
      title: '结束时间', //0：起草 1：审核中 2：有效 3：作废
      dataIndex: 'endTime',
      key: 'endTime',
      visible: true,
      width: 150,
      render: (text: string | null, record: Record<string, any>) => {
        return (
          <Tooltip title={text} placement="top">
            <span className={pageStyles['over-flow-hidden']} style={{ maxWidth: '100%' }} id={record?.id}>
              {text ? moment(text).format('YYYY-MM-DD') : '--'}
            </span>
          </Tooltip>
        );
      },
    },
    {
      title: '申请时间',
      dataIndex: 'createTime',
      key: 'createTime',
      visible: true,
      width: 150,
      sorter: (a: any, b: any) => (a?.createTime > b?.createTime ? 1 : -1),
      render: (text: string) => removeDateHmsStr(text) || '--',
    },
    {
      title: '申请人单位',
      dataIndex: 'createCompanyName',
      key: 'createCompanyName',
      visible: false,
      width: 150,
      sorter: (a: any, b: any) => (a?.createCompanyName > b?.createCompanyName ? 1 : -1),
      render: (text: string | null) => ColumnsRenderFun({ text, isTooltip: true }),
    },
    {
      title: '申请人部门',
      dataIndex: 'createDepartmentName',
      key: 'createDepartmentName',
      visible: false,
      width: 150,
      sorter: (a: any, b: any) => (a?.createDepartmentName > b?.createDepartmentName ? 1 : -1),
      render: (text: string | null) => ColumnsRenderFun({ text, isTooltip: true }),
    },
    {
      title: '申请人岗位',
      dataIndex: 'createPositionName',
      key: 'createPositionName',
      visible: false,
      width: 150,
      sorter: (a: any, b: any) => (a?.createPositionName > b?.createPositionName ? 1 : -1),
      render: (text: string | null) => ColumnsRenderFun({ text, isTooltip: true }),
    },
    {
      title: '申请人',
      dataIndex: 'createrName',
      key: 'createrName',
      visible: true,
      width: 140,
      sorter: (a: any, b: any) => (a?.createrName > b?.createrName ? 1 : -1),
      render: (text: string | null) => ColumnsRenderFun({ text, isTooltip: true }),
    },
    {
      title: '修改时间',
      dataIndex: 'modifyTime',
      key: 'modifyTime',
      visible: false,
      width: 150,
      render: (text: number | null) => stateOfRendering(text),
    },
    {
      title: '修改人',
      dataIndex: 'modifyerID',
      key: 'modifyerID',
      visible: false,
      width: 150,
      render: (text: number | null) => stateOfRendering(text),
    },
    {
      title: '修改人岗位',
      dataIndex: 'modifyPositionID',
      key: 'modifyPositionID',
      visible: false,
      width: 150,
      render: (text: number | null) => stateOfRendering(text),
    },
    {
      title: '修改人部门',
      dataIndex: 'modifyDepartmentID',
      key: 'modifyDepartmentID',
      visible: false,
      width: 150,
      render: (text: number | null) => stateOfRendering(text),
    },
    {
      title: '修改人单位',
      dataIndex: 'modifyCompanyID',
      key: 'modifyCompanyID',
      visible: false,
      width: 150,
      render: (text: number | null) => stateOfRendering(text),
    },
    {
      title: '创建人',
      dataIndex: 'createrID',
      key: 'createrID',
      visible: false,
      width: 150,
      render: (text: number | null) => stateOfRendering(text),
    },
    {
      title: '创建人岗位',
      dataIndex: 'createPositionID',
      key: 'createPositionID',
      visible: false,
      width: 150,
      render: (text: number | null) => stateOfRendering(text),
    },
    {
      title: '创建人部门',
      dataIndex: 'createDepartmentID',
      key: 'createDepartmentID',
      visible: false,
      width: 150,
      render: (text: number | null) => stateOfRendering(text),
    },
    {
      title: '创建人单位',
      dataIndex: 'createCompanyID',
      key: 'createCompanyID',
      visible: false,
      width: 150,
      render: (text: number | null) => stateOfRendering(text),
    },
    {
      title: '作废时间',
      dataIndex: 'cancelTime',
      key: 'createDepartmentID',
      visible: false,
      width: 150,
      render: (text: number | null) => stateOfRendering(text),
    },
    {
      title: '作废人',
      dataIndex: 'cancelerID',
      key: 'cancelerID',
      visible: false,
      width: 150,
      render: (text: number | null) => stateOfRendering(text),
    },
    {
      title: '作废人岗位',
      dataIndex: 'cancelPositionID',
      key: 'cancelPositionID',
      visible: false,
      width: 150,
      render: (text: number | null) => stateOfRendering(text),
    },
    {
      title: '作废人部门',
      dataIndex: 'cancelDepartmentID',
      key: 'cancelDepartmentID',
      visible: false,
      width: 150,
      render: (text: number | null) => stateOfRendering(text),
    },
    {
      title: '作废人单位',
      dataIndex: 'cancelCompanyID',
      key: 'cancelCompanyID',
      visible: false,
      width: 150,
      render: (text: number | null) => stateOfRendering(text),
    },
    {
      title: '作废原因',
      dataIndex: 'cancelReason',
      key: 'cancelReason',
      visible: false,
      width: 150,
      render: (text: number | null) => stateOfRendering(text),
    },
    {
      title: '备注',
      dataIndex: 'remark',
      key: 'remark',
      visible: false,
      width: 150,
      render: (text: number | null) => stateOfRendering(text),
    },
    {
      title: '经办时间',
      dataIndex: 'ownTime',
      key: 'ownTime',
      visible: false,
      width: 150,
      render: (text: number | null) => stateOfRendering(text),
    },
    {
      title: '经办人',
      dataIndex: 'ownerID',
      key: 'ownerID',
      visible: false,
      width: 150,
      render: (text: number | null) => stateOfRendering(text),
    },
    {
      title: '经办人岗位',
      dataIndex: 'ownPositionID',
      key: 'ownPositionID',
      visible: false,
      width: 150,
      render: (text: number | null) => stateOfRendering(text),
    },
    {
      title: '经办人部门',
      dataIndex: 'ownDepartmentID',
      key: 'ownDepartmentID',
      visible: false,
      width: 150,
      render: (text: number | null) => stateOfRendering(text),
    },
    {
      title: '经办人单位',
      dataIndex: 'ownCompanyID',
      key: 'ownCompanyID',
      visible: false,
      width: 150,
      render: (text: number | null) => stateOfRendering(text),
    },
    {
      title: '操作',
      dataIndex: 'operation',
      key: 'operation',
      width: 180,
      visible: true,
      unDrag: true,
      align: 'center',
      fixed: 'right',
      render: (_: string, record: Record<string, any>) => {
        return (
          <>
            <div
              style={{
                display: 'flex',
              }}
            >
              {/* {curRes?.['/assetInventory/uploadFile'] && record?.state == `${getTenantID()}${ILoadMapTypeAll.ASSET_INPLAY_STATUS}1003` ? (
                <UploadFormCommon
                  form={''}
                  fileFormName={''}
                  spanCol={24}
                  inRowProportion={24}
                  name={'attachFile'}
                  required={false}
                  accept={'.db'}
                  url={'/purchaseSourceController/uploadFile'}
                  downloadUrl={'/assetInfo/downloadFile'}
                  fileFormDelName={'delFile'}
                  uploadSuccessCallback={(attachPaths) => {
                    // setUploadUrl(attachPaths);
                  }}
                  defaultFiles={''}
                  maxFile={1}
                  hintTitle={'且只能上传 1 个！'}
                  fieldBusiPath={'reportURL'}
                  // isShowUploadBtn={true}
                  delFunc={(delFilePath) => {
                    // setUploadUrl('');
                  }}
                >
                  导入
                </UploadFormCommon>
              ) : (
                ''
              )} */}
              {Operation(curRes, operateFuns, record)}
            </div>
          </>
        );
      },
    },
  ];
};

export { getTableColumns };
